package com.stu.wgt.test.service.Imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.stu.wgt.test.bean.Member;
import com.stu.wgt.test.mapper.MemberMapper;
import com.stu.wgt.test.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wgt
 * @since 2024-01-20
 */
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements MemberService {

    @Autowired
    MemberMapper memberMapper;

    @Override
    public int insert(Member member) {
        return memberMapper.insert(member);
    }

    @Override
    public int delete(int id) {
        return memberMapper.deleteById(id);
    }

    @Override
    public int update(Member member) {
        return memberMapper.updateById(member);
    }

    @Override
    public List<Member> selectAllMember() {
        return memberMapper.selectList(null);
    }

    @Override
    public List<Member> selectById(int id) {
        QueryWrapper<Member> qw = new QueryWrapper<>();
        qw.eq("id", id);
        return memberMapper.selectList(qw);
    }

    @Override
    public List<Member> selectByName(String username) {
        QueryWrapper<Member> qw = new QueryWrapper<>();
        qw.eq("name", username);
        return memberMapper.selectList(qw);
    }

    @Override
    public List<Member> selectByTelephone(String telephone) {
        QueryWrapper<Member> qw = new QueryWrapper<>();
        qw.eq("telephone", telephone);
        return memberMapper.selectList(qw);
    }

    @Override
    public IPage<Member> searchMember(String query, int pageNum, int pageSize) {
        Page<Member> page = new Page<>(pageNum, pageSize); // 创建分页对象，指定当前页码和每页显示条数
        QueryWrapper<Member> qw = new QueryWrapper<>(); // 创建查询条件对象
        qw.like("name", query); // 设置查询条件，这里使用模糊查询
        IPage<Member> memberPage = memberMapper.selectPage(page, qw); // 执行分页查询
        return memberPage;
    }

    @Override
    public boolean login(String name, String pass) {
        QueryWrapper<Member> qw = new QueryWrapper<>();
        qw.eq("name", name).eq("password", pass);
        List<Member> memberList = memberMapper.selectList(qw);
        if (!memberList.isEmpty()) {
            return true;
        }
        return false;
    }

    @Override
    public boolean register(Member member) {
        if (member.getPassword().equals(member.getConfigurePassword())) {
            return false;
        }
        QueryWrapper<Member> qw = new QueryWrapper<>();
        qw.eq("name", member.getName()).eq("password", member.getPassword());
        if (memberMapper.selectList(qw).isEmpty()) {
            memberMapper.insert(member);
            return true;
        }
        return false;
    }

}
